package com.iot.inclinometer.util;

import android.content.SharedPreferences;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;
/*
 * 测斜仪的相关操作
 */
public class InclinometerControl{
	/**
	 * 系统测试方法，返回结果
	 * @return	返回字符串，“稳定” OR “不稳定”
	 */

	public static final double  PI=3.1415926;  
  

	/**
	 * 校正监测模式：数据解算方法
	 */
	public static double[] getMeasureResult(double X,double Y,double Z,double T,int interval){
		double result[]=new double[4]; 
		SharedPreferences sharedPreferences=ContextApplication.getAppContext().getSharedPreferences("configuration",0);
		double A_X=Double.parseDouble(sharedPreferences.getString("A_X",null));
		double A_Y=Double.parseDouble(sharedPreferences.getString("A_Y",null));
		double A_Z=Double.parseDouble(sharedPreferences.getString("A_Z",null));
		double B_X=Double.parseDouble(sharedPreferences.getString("B_X",null));
		double B_Y=Double.parseDouble(sharedPreferences.getString("B_Y",null));
		double B_Z=Double.parseDouble(sharedPreferences.getString("B_Z",null));
		double Tclib=Double.parseDouble(sharedPreferences.getString("Tclib",null));
		double phiX=Double.parseDouble(sharedPreferences.getString("phiX",null));
		double[][] matA=new double[3][3];
		matA[0][0]=Double.parseDouble(sharedPreferences.getString("matA11",null));
		matA[0][1]=Double.parseDouble(sharedPreferences.getString("matA12",null));
		matA[0][2]=Double.parseDouble(sharedPreferences.getString("matA13",null));
		matA[1][0]=Double.parseDouble(sharedPreferences.getString("matA21",null));
		matA[1][1]=Double.parseDouble(sharedPreferences.getString("matA22",null));
		matA[1][2]=Double.parseDouble(sharedPreferences.getString("matA23",null));
		matA[2][0]=Double.parseDouble(sharedPreferences.getString("matA31",null));
		matA[2][1]=Double.parseDouble(sharedPreferences.getString("matA32",null));
		matA[2][2]=Double.parseDouble(sharedPreferences.getString("matA33",null));
		double dAX=Double.parseDouble(sharedPreferences.getString("dAX",null));
		double dAY=Double.parseDouble(sharedPreferences.getString("dAY",null));
		double dAZ=Double.parseDouble(sharedPreferences.getString("dAZ",null));
		double dBX=Double.parseDouble(sharedPreferences.getString("dBX",null));
		double dBY=Double.parseDouble(sharedPreferences.getString("dBY",null));
		double dBZ=Double.parseDouble(sharedPreferences.getString("dBZ",null));	   
		double dT=Tclib-T;//温度补偿
		double AZ_pie=A_Z-dT*dAZ;
		double BZ_pie=B_Z-dT*dBZ;
		double AX_pie=A_X-dT*dAX;
		double BX_pie=B_X-dT*dBX;
		double AY_pie=A_Y-dT*dAY;
		double BY_pie=B_Y-dT*dBY;
		double[][] ZXYa=new double[1][3];//归一化
		ZXYa[0][0]=(Z-BZ_pie)/AZ_pie;
		ZXYa[0][1]=(X-BX_pie)/AX_pie;
		ZXYa[0][2]=(Y-BY_pie)/AY_pie;
		double[][] ZXYb=new double[1][3];
		double[][] youshouxi=new double[3][3];
		double[][] ZXYc=new double[1][3];
		youshouxi[0][0]=-1.0;youshouxi[0][1]=0.0;youshouxi[0][2]=0.0;
		youshouxi[1][0]=0.0;youshouxi[1][1]=-1.0;youshouxi[1][2]=0.0;
		youshouxi[2][0]=0.0;youshouxi[2][1]=0.0;youshouxi[2][2]=-1.0;
		ZXYb=Matrix.Mult(ZXYa,youshouxi);//右手系变换
		ZXYc=Matrix.Mult(ZXYb,Matrix.Turn(Matrix.Inver(Matrix.Turn(matA))));//工装误差矫正
		//ZXYc=Matrix.Mult(ZXYb,Matrix.Inver(Matrix.Turn(matA)));
		double fwj=-(Math.atan(ZXYc[0][1]/ZXYc[0][0])-phiX);
		double tan_thetaZ=ZXYc[0][0]/ZXYc[0][2];
		double tan_thetaX=ZXYc[0][1]/ZXYc[0][2];
		double tan_theta = 0;
		if(tan_thetaX>0)
		{ 
			tan_theta=Math.sqrt(tan_thetaX*tan_thetaX+tan_thetaZ*tan_thetaZ);
		}else {
			tan_theta=-Math.sqrt(tan_thetaX*tan_thetaX+tan_thetaZ*tan_thetaZ);
		}
		
    	result[0]=Common.remainSixNum(Math.atan(tan_thetaX)); //角度为弧度，保留6位小数
    	result[1]=Common.remainSixNum(Math.atan(tan_thetaZ));	//在此程序后，讲Z一律看为Y  
    	result[2]=Common.remainSixNum(Math.atan(tan_theta));
    	result[3]=T;
    	return result;	
	}
	/**
	 * 实测监测模式：数据解算方法
	 */
	public static double[] getMeasureResult2(double X,double Y,double Z,double T,int interval,String qj){
		double result[]=new double[4]; 
		SharedPreferences sharedPreferences=ContextApplication.getAppContext().getSharedPreferences("configuration",0);
		double A_X=Double.parseDouble(sharedPreferences.getString("A_X",null));
		double A_Y=Double.parseDouble(sharedPreferences.getString("A_Y",null));
		double A_Z=Double.parseDouble(sharedPreferences.getString("A_Z",null));
		double B_X=Double.parseDouble(sharedPreferences.getString("B_X",null));
		double B_Y=Double.parseDouble(sharedPreferences.getString("B_Y",null));
		double B_Z=Double.parseDouble(sharedPreferences.getString("B_Z",null));
		double Tclib=Double.parseDouble(sharedPreferences.getString("Tclib",null));
		double phiX=Double.parseDouble(sharedPreferences.getString("phiX",null));
		double[][] matA=new double[3][3];
		matA[0][0]=Double.parseDouble(sharedPreferences.getString("matA11",null));
		matA[0][1]=Double.parseDouble(sharedPreferences.getString("matA12",null));
		matA[0][2]=Double.parseDouble(sharedPreferences.getString("matA13",null));
		matA[1][0]=Double.parseDouble(sharedPreferences.getString("matA21",null));
		matA[1][1]=Double.parseDouble(sharedPreferences.getString("matA22",null));
		matA[1][2]=Double.parseDouble(sharedPreferences.getString("matA23",null));
		matA[2][0]=Double.parseDouble(sharedPreferences.getString("matA31",null));
		matA[2][1]=Double.parseDouble(sharedPreferences.getString("matA32",null));
		matA[2][2]=Double.parseDouble(sharedPreferences.getString("matA33",null));
		double dAX=Double.parseDouble(sharedPreferences.getString("dAX",null));
		double dAY=Double.parseDouble(sharedPreferences.getString("dAY",null));
		double dAZ=Double.parseDouble(sharedPreferences.getString("dAZ",null));
		double dBX=Double.parseDouble(sharedPreferences.getString("dBX",null));
		double dBY=Double.parseDouble(sharedPreferences.getString("dBY",null));
		double dBZ=Double.parseDouble(sharedPreferences.getString("dBZ",null));	   
		double dT=Tclib-T;//温度补偿
		double AZ_pie=A_Z-dT*dAZ;
		double BZ_pie=B_Z-dT*dBZ;
		double AX_pie=A_X-dT*dAX;
		double BX_pie=B_X-dT*dBX;
		double AY_pie=A_Y-dT*dAY;
		double BY_pie=B_Y-dT*dBY;
		double[][] ZXYa=new double[1][3];//归一化
		ZXYa[0][0]=(Z-BZ_pie)/AZ_pie;
		ZXYa[0][1]=(X-BX_pie)/AX_pie;
		ZXYa[0][2]=(Y-BY_pie)/AY_pie;
		double[][] ZXYb=new double[1][3];
		double[][] youshouxi=new double[3][3];
		double[][] ZXYc=new double[1][3];
		youshouxi[0][0]=-1.0;youshouxi[0][1]=0.0;youshouxi[0][2]=0.0;
		youshouxi[1][0]=0.0;youshouxi[1][1]=-1.0;youshouxi[1][2]=0.0;
		youshouxi[2][0]=0.0;youshouxi[2][1]=0.0;youshouxi[2][2]=-1.0;
		ZXYb=Matrix.Mult(ZXYa,youshouxi);//右手系变换
		ZXYc=Matrix.Mult(ZXYb,Matrix.Turn(Matrix.Inver(Matrix.Turn(matA))));//工装误差矫正
		//ZXYc=Matrix.Mult(ZXYb,Matrix.Inver(Matrix.Turn(matA)));
		double fwj=-(Math.atan(ZXYc[0][1]/ZXYc[0][0])-phiX);
		double tan_thetaZ=ZXYc[0][0]/ZXYc[0][2];
		double tan_thetaX=ZXYc[0][1]/ZXYc[0][2];
		double tan_theta = 0;
		if(qj.equals("A"))
		{
			if(tan_thetaX>=0)
			{ 
				tan_theta=Math.sqrt(tan_thetaX*tan_thetaX+tan_thetaZ*tan_thetaZ);
			}else {
				tan_theta=-Math.sqrt(tan_thetaX*tan_thetaX+tan_thetaZ*tan_thetaZ);
			}
		}else {
			if(tan_thetaZ>=0)
			{ 
				tan_theta=Math.sqrt(tan_thetaX*tan_thetaX+tan_thetaZ*tan_thetaZ);
			}else {
				tan_theta=-Math.sqrt(tan_thetaX*tan_thetaX+tan_thetaZ*tan_thetaZ);
			}
		}
		
		
    	result[0]=Common.remainSixNum(Math.atan(tan_thetaX)); //角度为弧度，保留6位小数
    	result[1]=Common.remainSixNum(Math.atan(tan_thetaZ));	//在此程序后，讲Z一律看为Y  
    	result[2]=Common.remainSixNum(Math.atan(tan_theta));
    	result[3]=T;
    	return result;	
	}
	/*
	 * 计算A/B方向位移与角度
	 * 参数：角度的单位为弧度，间距单位为cm*/
	public static Map<String,double[]> leijiData(double[] thetaX0,double[] thetaY0,double[] thetaX180,double[] thetaY180,int interval){
		double L=(double) (interval*0.01);//cm转m
		int n=thetaX0.length;
		double[] thetaX=new double[n];//A方向的角度
		Log.i("info", "l:"+L);
		double[] thetaY=new double[n];//B方向的角度
		double[] leijilx=new double[n];//A方向的角度 :累计位移
		double[] leijily=new double[n];//B方向的角度
		for(int i=0;i<n;i++){
			thetaX[i]=(thetaX0[i]-thetaX180[i])/2;
			thetaY[i]=(thetaY0[i]-thetaY180[i])/2;
		}
		/*第一次**/
	//	thetaX[0]=(float) (0.90945*3.1415926/180);thetaX[1]=(float) (1.26275*3.1415926/180);thetaX[2]=(float) (1.13345*3.1415926/180);
	//	thetaX[3]=(float) (0.5566*3.1415926/180);thetaX[4]=(float) (0.4374*3.1415926/180);thetaX[5]= (float) (0.3968*3.1415926/180);
	//	thetaY[0]=(float) (0.534*3.1415926/180);thetaY[1]=(float) (0.6364*3.1415926/180);thetaY[2]=(float) (.28145*3.1415926/180);
	//	thetaY[3]=(float) (-0.24265*3.1415926/180);thetaY[4]=(float) (-0.2076*3.1415926/180);thetaY[5]=(float) (-0.1846*3.1415926/180);
		/*第2次**/
	//	thetaX[0]=(float) (0.8923*3.1415926/180);thetaX[1]=(float) (1.28885*3.1415926/180);thetaX[2]=(float) (1.152*3.1415926/180);
	//	thetaX[3]=(float) (0.57745*3.1415926/180);thetaX[4]=(float) (0.4363*3.1415926/180);thetaX[5]=(float) (0.4196*3.1415926/180);
	//	thetaY[0]=(float) (0.55085*3.1415926/180);thetaY[1]=(float) (0.65645*3.1415926/180);thetaY[2]=(float) (0.29015*3.1415926/180);
	//	thetaY[3]=(float) (-0.21925*3.1415926/180);thetaY[4]=(float) (-0.18995*3.1415926/180);thetaY[5]=(float) (-0.1703*3.1415926/180);
		/*第3次**/
	//	thetaX[0]=(float) (0.7645*3.1415926/180);thetaX[1]=(float) (1.1294*3.1415926/180);thetaX[2]=(float) (1.2587*3.1415926/180);
	//	thetaX[3]=(float) (1.3897*3.1415926/180);thetaX[4]=(float) (1.294*3.1415926/180);thetaX[5]=(float) (0.1758*3.1415926/180);
	//	thetaY[0]=(float) (0.5836*3.1415926/180);thetaY[1]=(float) (0.68395*3.1415926/180);thetaY[2]=(float) (0.1628*3.1415926/180);
	//	thetaY[3]=(float) (-0.8031*3.1415926/180);thetaY[4]=(float) (-0.8096*3.1415926/180);thetaY[5]=(float) (-0.0698*3.1415926/180);
		for(int i=0;i<n;i++){
	    	if(i==0){
	    		//第一个数据为其本身
	    		leijilx[i] = Math.sin(thetaX[i])*L;//计算位移  
	    		leijily[i] = Math.sin(thetaY[i])*L;
	    	}else{
	    		//从第二个数据开始，累加位移(随深度)
	    		leijilx[i] = Math.sin(thetaX[i])*L+leijilx[i-1];//计算位移  
	    		leijily[i] = Math.sin(thetaY[i])*L+leijily[i-1];
	    	}
		}
		Map<String,double[]> result=new HashMap<String, double[]>();
		result.put("lx",leijilx);//A方向累计位移
		result.put("ly",leijily);//B方向累计位移
		result.put("thetaX",thetaX);//A方向的角度
		result.put("thetaY",thetaY);//B方向的角度
		return result;
	}
}
